package com.ruoyi.bysj.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.bysj.domain.SysDeliveryGoods;
import com.ruoyi.bysj.domain.SysGoods;
import com.ruoyi.bysj.mapper.SysDeliveryGoodsMapper;
import com.ruoyi.bysj.mapper.SysGoodsMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserRole;
import org.apache.ibatis.annotations.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bysj.mapper.SysDeliveryMapper;
import com.ruoyi.bysj.domain.SysDelivery;
import com.ruoyi.bysj.service.ISysDeliveryService;

/**
 * 货物配送Service业务层处理
 * 
 * @author laow
 * @date 2021-10-19
 */
@Service
public class SysDeliveryServiceImpl implements ISysDeliveryService 
{
    @Autowired
    private SysDeliveryMapper sysDeliveryMapper;

    @Autowired
    private SysDeliveryGoodsMapper sysDeliveryGoodsMapper;

    @Autowired
    private SysGoodsMapper sysGoodsMapper;

    /**
     * 查询货物配送
     * 
     * @param deliveryId 货物配送主键
     * @return 货物配送
     */
    @Override
    public SysDelivery selectSysDeliveryByDeliveryId(Long deliveryId)
    {
   /*     SysDelivery sysDelivery = sysDeliveryMapper.selectSysDeliveryByDeliveryId(deliveryId);
        Long[] goodsIds = sysDeliveryGoodsMapper.selectDeliveryGoodsByDeliveryId(deliveryId);
        String[] goodsNames = null;
        for (Long goodsId : goodsIds) {
            sysGoodsMapper.selectSysGoodsByGoodsId(goodsId).getGoodsName();
        }*/


        return sysDeliveryMapper.selectSysDeliveryByDeliveryId(deliveryId);
    }

    /**
     * 查询货物配送列表
     * 
     * @param sysDelivery 货物配送
     * @return 货物配送
     */
    @Override
    public List<SysDelivery> selectSysDeliveryList(SysDelivery sysDelivery)
    {
        List<SysDelivery> deliveryList = sysDeliveryMapper.selectSysDeliveryList(sysDelivery);

        for (SysDelivery delivery : deliveryList) {
            List<String> goodsNames = null;
            List<Long> goodsIds = sysDeliveryGoodsMapper.selectDeliveryGoodsByDeliveryId(delivery.getDeliveryId());
            delivery.setGoodsIds(goodsIds);
            /*for (String goodsName : goodsNames) {
                for (Long goodsId : goodsIds) {
                    goodsName = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId).getGoodsName();
                }
            }
            delivery.setGoodsNames(goodsNames);*/
        }
        return deliveryList;
    }

   /* @Override
    public String selectDeliverygoodsGroup(Long deliveryId)
    {
        List<SysGoods> goodsList = null;
        Long[] goodsIds = sysDeliveryGoodsMapper.selectDeliveryGoodsByDeliveryId(deliveryId);
        for (Long goodsId : goodsIds) {
            goodsList =  sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
        }
        StringBuffer idsStr = new StringBuffer();
        for (SysGoods sysGoods : goodsList) {
            idsStr.append(sysGoods.getGoodsName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString()))
        {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }*/
    /**
     * 新增货物配送
     * 
     * @param sysDelivery 货物配送
     * @return 结果
     */
    @Override
    public int insertSysDelivery(SysDelivery sysDelivery)
    {
        sysDelivery.setCreateTime(DateUtils.getNowDate());
        int rows = sysDeliveryMapper.insertSysDelivery(sysDelivery);
        insertDeliverygoods(sysDelivery);
        return rows;
    }

    /**
     * 修改货物配送
     * 
     * @param sysDelivery 货物配送
     * @return 结果
     */
    @Override
    public int updateSysDelivery(SysDelivery sysDelivery)
    {
        sysDelivery.setDeliveryTime(DateUtils.getNowDate());
        sysDelivery.setDeliveryState("2");
        return sysDeliveryMapper.updateSysDelivery(sysDelivery);
    }

    /**
     * 批量删除货物配送
     * 
     * @param deliveryIds 需要删除的货物配送主键
     * @return 结果
     */
    @Override
    public int deleteSysDeliveryByDeliveryIds(Long[] deliveryIds)
    {
        return sysDeliveryMapper.deleteSysDeliveryByDeliveryIds(deliveryIds);
    }

    /**
     * 删除货物配送信息
     * 
     * @param deliveryId 货物配送主键
     * @return 结果
     */
    @Override
    public int deleteSysDeliveryByDeliveryId(Long deliveryId)
    {
        return sysDeliveryMapper.deleteSysDeliveryByDeliveryId(deliveryId);
    }

    @Override
    public List<Long> selectGoodsListBydeliveryId(Long deliveryId) {
        return sysDeliveryGoodsMapper.selectDeliveryGoodsByDeliveryId(deliveryId);
    }

    @Override
    public List<SysGoods> selectGoodsListsBydeliveryId(Long deliveryId) {
        List<Long> goodsIds = sysDeliveryGoodsMapper.selectDeliveryGoodsByDeliveryId(deliveryId);
        List<SysGoods> goods = new ArrayList<SysGoods>();
        for (Long goodsId : goodsIds) {
            SysGoods g = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
            goods.add(g);
        }
        return goods;
    }
    public void insertDeliverygoods(SysDelivery sysDelivery)
    {
        List<Long> goods = sysDelivery.getGoodsIds();
        if (StringUtils.isNotNull(goods))
        {
            List<SysDeliveryGoods> list = new ArrayList<SysDeliveryGoods>();
            for (Long goodsId : goods)
            {
                SysDeliveryGoods dg = new SysDeliveryGoods();
                dg.setDeliveryId(sysDelivery.getDeliveryId());
                dg.setGoodsId(goodsId);
                list.add(dg);
            }
            if (list.size() > 0)


            {
                sysDeliveryGoodsMapper.batchDeliveryGoods(list);
            }
        }
    }
}
